var home = require("../../utils/util.js");
var url = home.home;
var url2 = home.home2;
var app = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
      //members:['你个倔强的小萝卜'],
      isCollection: false,
      play:false,
      type: 1,
      type1: 0,
      vipId: '',
      vipCardName: '',
      vipCardDetailUrl: '',
      shopData: {},
      guestData: [], //评论列表数据
      tokerData: {}, //贵宾卡数据
      storeId: app.globalData.storeId,
      toReceiveVipId: '',
      states: 0,
      shipinid: 0,
      shipin: '',//https://www.kyz66.com/temp/2_1681818098864.mp4
      memberId: home.getKey('userId'),
      phone: home.getKey('phone'),
      loadState: true,
      fabulous: 0,
      isFabulous: false,
      fenxiang: 0,
      comments: 0,
    },
    //分享url：vipId=member_share_vip(ID)&type=4
    //小程序码url:vipId=member_to_receive_vip(ID)&type=3
    onLoad: function (options) {
      //console.log(options.shipinid)
      let scene = decodeURIComponent(options.scene);
      let type1 = options.type;
      if (options.type == 4 || options.type == 5) {
        type1 = 2;
      }
      if (options.type == 6) {
        type1 = 3;
      }
      if (scene != 'undefined') {
        let arr = scene.split('&');
        let arr1 = arr[0];
        let arr2 = arr[1];
        //let arr3 = arr[2];
        let vipId = arr1.split('=')[1];
        let type = arr2.split('=')[1];
        //let videoid = arr3.split('=')[1];
        options.id = vipId;//卡下发记录表里有视频卡Id
        options.type = type;
        type1 = type;
        //shipinid = videoid;//新加视频卡id
      }
      this.setData({
        type: options.type == 6 ? 3 : options.type,
        vipId: options.id ? options.id : options.vipId,
        type1: type1
      });
      console.log('--------vipId=' +this.data.vipId+'&type='+ this.data.type)
      //查询视频卡id
      let that = this;
      wx.request({
        url: url + 'app/getShareVideoDetail?vipId=' + this.data.vipId + '&type=' + this.data.type,//李哥新增加接口
        //url: url + 'app/getVipDetail?vipId=1649775177165774849&type=2',
        data: {
          //vipId: '1649775177165774849',
          //type: 2
          vipId: this.data.vipId,
          type: this.data.type
        },
        method: 'GET',
        header: {
          "content-type": "application/json", // 默认
        },
        success: function (res) {
          if (res.data.code == 200) {
            that.setData({
              type: options.type == 6 ? 3 : options.type,
              vipId: options.id ? options.id : options.vipId,
              type1: type1,
              //shipin: 'https://kyz.fbhkj.com//' + res.data.result.vedioPath,//视频路径
              shipin: 'https://kyz.fbhkj.com/' + res.data.result.vedioPath,//视频路径
              shipinid: res.data.result.id,//接口返回
              //shipinid: '1649771664582975489'//shipinid
              tokerData: res.data.result,
              guestData: res.data.result.comments,
              fabulous: res.data.result.praiseNum,
              fenxiang: res.data.result.forwardNum,
              storeId: res.data.result.storeId,
              vipCardName: res.data.result.vipCardName,
              vipCardDetailUrl: res.data.result.vipCardDetailUrl,
            });
            //that.getGuestInfo();
          } else {
            home.wxMessage('未找到视频ID', 'none');
          }
        }
      })
      //查询视频卡id
    },

  //跳转贵宾卡页
  openka(){
    wx.navigateTo({
      url: '../../shop/guest/info?type='+this.data.type+'&id='+this.data.vipId,
    })
  },
  //获取贵宾卡视频信息
  getGuestInfo() {
    let that = this;
    wx.request({
      url: url + 'app/queryById?id=' + that.data.shipinid,
      data: {
        id: that.data.shipinid,
      },
      method: 'GET',
      header: {
        "content-type": "application/json", // 默认
      },
      success: function (res) {
        if (res.data.code == 200) {
          //接口传递过来，分组//王重阳
          that.setData({
            tokerData: res.data.result,
            guestData: res.data.result.comments,
            fabulous: res.data.result.praiseNum,
            fenxiang: res.data.result.forwardNum,
            storeId: res.data.result.storeId,
            video: res.data.result.vedioPath,
          });
          home.setKey('storeId', res.data.result.storeId);
          that.getShop();
        } else {
          home.wxMessage('参数错误', 'none');
        }
      }
    })
  },

  //获取店铺信息
  getShop() {
    let that = this;
    wx.request({
      url: url + 'app/getStore',
      data: {
        storeId: that.data.storeId
      },
      method: 'GET',
      header: {
        "content-type": "application/json", // 默认
      },
      success: function (res) {
        if (res.data.code == 200) {
          that.setData({
            shopData: res.data.result,
          })
        } else {
          wx.showToast({
            title: '店铺信息获取失败',
            icon: 'none'
          })
        }
      }
    })
  },
  register() {
    let that = this;
    wx.login({
      success: function name(res) {
        let data = {};
        data.code = res.code;
        data.storeId = that.data.storeId;
        if (that.data.type == 3 || that.data.type == 4) {
          data.loginType = 2;
        }
        console.log(data);
        wx.request({
          url: url + 'app/xcx/register?storeId=' + app.globalData.storeId + '&code=' + res.code,
          data: data,
          method: 'POST',
          header: {
            "content-type": "application/json", // 默认
          },
          success: function (rea) {
            if (rea.data.code == 200) {
              let states = 2;
              if (!rea.data.result.hasPhone) {
                states = 0;
              }
              home.setKey('hasNickName', rea.data.result.hasNickName);
              home.setKey('hasPhone', rea.data.result.hasPhone);
              home.setKey('userId', rea.data.result.member.id);
              home.setKey('phone', rea.data.result.member.phone);
              home.setKey('avatar', rea.data.result.member.avatarUrl);
              home.setKey('status', rea.data.result.member.status);
              home.setKey('discount', rea.data.result.member.discount);
              home.setKey('nickName', rea.data.result.member.nickName);
              home.setKey('openId', rea.data.result.member.xcxOpenId);
              home.setKey('unionId', rea.data.result.member.unionId);
              that.setData({
                states: states,
                memberId: rea.data.result.member.id,
                phone: rea.data.result.member.phone
              });
              wx.hideLoading({
                success: (res) => {
                  that.setData({
                    loadState:false,
                  })
                },
              })
            } else {
              home.wxMessage(rea.data.message,'none');
            }
          }
        })
      }
    })
  },

  //获取手机号
  getPhoneNumber(e) {
    if (e.detail.code == undefined) {
      home.wxMessage('请授权获取手机号', 'none');
      wx.setStorageSync('state', 1);
      return false;
    }
    let that = this;
    wx.request({
      url: 'https://kyz.fbhkj.com/fubaihui/app/xcx/getPhone?storeId=' + app.globalData.storeId + '&code=' + e.detail.code + '&xcxOpenId=' + wx.getStorageSync('openId') + '&unionId=' + wx.getStorageSync('unionId'),
      data: {
        storeId: app.globalData.storeId,
        code: e.detail.code,
        xcxOpenId: wx.getStorageSync('openId'),
        unionId: wx.getStorageSync('unionId')
      },
      method: 'POST',
      header: {
        "content-type": "application/json", // 默认
      },
      success: function (res) {
        if (res.data.code == 200) {
          home.setKey('userId',res.data.result.id);
          that.setData({
            states: 2,
            memberId: res.data.result.id
          })
        } else {
          home.wxMessage('授权失败', 'none');
        }
      }
    })
  },

    //点击暂停/开始
    videoTap:function(){
      //获取video
      this.videoContext = wx.createVideoContext('myVideo', this);
      if (this.data.play){
        this.videoContext.play();
        this.setData({
          play:false
        })
      }else{
        this.videoContext.pause();
        this.setData({
          play: true
        })
      }
    },

    collection: function () {
      var isFabulous = this.data.isFabulous;
      var fabulous = this.data.fabulous;

      if (typeof (fabulous) != 'string') {
        fabulous = "" + fabulous;
      }
      var e = fabulous.match(/[a-z]/ig);  // 匹配字母
      if (!!e) {//包含字母
        var fabulousNum = fabulous;
      } else {
        var fabulousNum = isFabulous ? Number(fabulous) - 1 : Number(fabulous) + 1;
      }
      
      this.setData({
        isCollection: !this.data.isCollection,
        fabulous: fabulousNum,
        isFabulous: !isFabulous
      }, () => {
        if (this.data.isCollection) {
          //console.log('记录点赞表')
          wx.request({
            url: url + 'app/addPraiseNum?id=' + this.data.shipinid,
            data: {
              id: this.data.shipinid,
            },
            method: 'GET',
            header: {
              "content-type": "application/json", // 默认
            },
            success: function (res) {

            }
          })
          // wx.showToast({
          //   title: '谢谢您的点赞！',
          //   icon: 'none'
          // })
        }
      })
    },
    pinglun: function () {
      wx.showToast({
        title: '领取后即可评论',
        icon: 'none'
      })
    },

    onShow: function () {
      // this.videoContext = wx.createVideoContext('myVideo', this);
      // this.videoContext.requestFullScreen({
      //     direction: -90,//90
      // });
      this.setData({
        memberId: home.getKey('userId'),
        storeId: app.globalData.storeId,
        phone: home.getKey('phone'),
        loadState: true
      })
      //this.getGuestInfo();
      this.register();
      wx.showLoading({
        title: '加载中...',
      });
    },

  videoErrorCallback(e) {
      console.log('Video error info:')
      console.log(e.detail)
   },

    //分享
    onShareAppMessage: function (ops) {
      let that = this;
      return {
        title: that.data.vipCardName,
        imageUrl: 'https://kyz.fbhkj.com//' + that.data.vipCardDetailUrl,
        path: '/pages/shipin/index?vipId='+that.data.vipId+'&type='+that.data.type,
        success: function (res) {
          console.log(res, "转发成功")
          wx.request({
            url: url + 'app/addForwardNum?id=' + that.data.shipinid,
            data: {
              id: that.data.shipinid,
            },
            method: 'GET',
            header: {
              "content-type": "application/json", // 默认
            },
            success: function (res) {
              // 转发成功之后的回调
              console.log('fxok');
              that.setData({
                fenxiang: 4,
              })
            }
          })
        },
        fail: function (res) {
          console.log(res, "转发失败")
        }
      }
    },

    onReady: function (params) {

    }
})